package com.example.android.ui.searchHB.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.android.R;
import com.example.android.bean.Goods;
import com.example.android.bean.SearchHB;
import com.example.android.ui.hb.Adapter.HBRecyclerViewAdapter;

import java.util.List;

public class SearchHBRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<SearchHB> searchHBList;
    private Context context;
    private LayoutInflater inflater;
    private OnItemClickListener listener;
    private RecyclerView recyclerView;
    public SearchHBRecyclerViewAdapter(RecyclerView recyclerView, Context context,List<SearchHB> searchHBList){
        this.recyclerView = recyclerView;
        this.context =context;
        inflater = LayoutInflater.from(context);
        this.searchHBList = searchHBList;
    }
    public void setSearchHB(List<SearchHB> searchHBList){
        this.searchHBList.clear();
        this.searchHBList.addAll(searchHBList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =inflater.inflate(R.layout.search_hb_item,parent,false);
        view.setOnClickListener(this);
        return new SearchHBViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            SearchHB searchHB = searchHBList.get(position);
            SearchHBViewHolder search = (SearchHBViewHolder) holder;
            search.name.setText(searchHB.getHbName());
            search.content.setText(searchHB.getHbIntroduction());
            search.reward.setText(searchHB.getHbReward()+"");
            search.deadline.setText(searchHB.getHbReleaseDeadlineDatetime());
            Glide.with(context).load(searchHB.getHbImagePath()).into(search.image);
    }

    @Override
    public int getItemCount() {
        return searchHBList.size();
    }

    @Override
    public void onClick(View view) {
        if (listener != null) {
            int position = recyclerView.getChildAdapterPosition(view);
            listener.onItemClick(searchHBList.get(position));
        }
    }

    class SearchHBViewHolder extends RecyclerView.ViewHolder{
        private TextView name,content,reward,deadline;
        private ImageView image;

        public SearchHBViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.search_hb_item_name);
            content = itemView.findViewById(R.id.search_hb_item_content);
            reward = itemView.findViewById(R.id.search_hb_item_reward);
            deadline = itemView.findViewById(R.id.search_hb_item_deadline);
            image = itemView.findViewById(R.id.search_hb_item_image);

        }
    }

    public interface OnItemClickListener{
        void onItemClick(SearchHB searchHB);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
